﻿using System;
using App.Entity;
using App.IServices;
using App.IRepository;
using App.Code;
using System.Collections.Generic;

/*!
* 文件描述：一切从简，只为了更懒！
*/
namespace App.Services
{
	public partial class Sys_UserServices : BaseServices<Sys_User> , ISys_UserServices
	{
		ISys_UserRepository _ISys_UserRepository;
        ISys_UserLogOnServices _userLogDal;
        public Sys_UserServices(ISys_UserRepository __ISys_UserRepository, ISys_UserLogOnServices userLogDal)
		{
			this._ISys_UserRepository = __ISys_UserRepository;
			base._IBaseRepository = __ISys_UserRepository;
            _userLogDal = userLogDal;

        }

        public Sys_User CheckLogin(string username, string password)
        {
            Sys_User userEntity = _ISys_UserRepository.QuerySingle(t => t.Account == username);
            if (userEntity != null)
            {
                if (userEntity.EnabledMark == true)
                {
                    Sys_UserLogOn userLogOnEntity = _userLogDal.GetForm(userEntity.Id);
                    string dbPassword = Md5.md5(DESEncrypt.Encrypt(password.ToLower(), userLogOnEntity.UserSecretkey).ToLower(), 32).ToLower();
                    if (dbPassword == userLogOnEntity.UserPassword)
                    {
                        DateTime lastVisitTime = DateTime.Now;
                        int LogOnCount = (userLogOnEntity.LogOnCount).ToInt() + 1;
                        if (userLogOnEntity.LastVisitTime != null)
                        {
                            userLogOnEntity.PreviousVisitTime = userLogOnEntity.LastVisitTime.ToDate();
                        }
                        userLogOnEntity.LastVisitTime = lastVisitTime;
                        userLogOnEntity.LogOnCount = LogOnCount;
                        _userLogDal.UpdateForm(userLogOnEntity);
                        return userEntity;
                    }
                    else
                    {
                        throw new Exception("密码不正确，请重新输入");
                    }
                }
                else
                {
                    throw new Exception("账户被系统锁定,请联系管理员");
                }
            }
            else
            {
                throw new Exception("账户不存在，请重新输入");
            }
        }

        public void DeleteForm(string keyValue)
        {
            _ISys_UserRepository.Delete(c => c.Id == keyValue);
        }

        public Sys_User GetForm(string keyValue)
        {
            return _ISys_UserRepository.QuerySingle(c => c.Id == keyValue);
        }

        public List<Sys_User> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<Sys_User>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.Account.Contains(keyword));
                expression = expression.Or(t => t.RealName.Contains(keyword));
                expression = expression.Or(t => t.MobilePhone.Contains(keyword));
            }
            expression = expression.And(t => t.Account != "admin");
            return _ISys_UserRepository.QueryByWherePage(pagination.page, pagination.rows, expression, "CreatorTime");
        }

        public Sys_User GetUser(string userName)
        {
            return _ISys_UserRepository.QuerySingle(c => c.Account == userName);
        }

        public void SubmitForm(Sys_User userEntity, Sys_UserLogOn userLogOnEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                userEntity.Modify(keyValue);
                _ISys_UserRepository.Update(userEntity);
            }
            else
            {
                userEntity.Create();
                userLogOnEntity.Id = userEntity.Id;
                userLogOnEntity.UserId = userEntity.Id;
                userLogOnEntity.UserSecretkey = Md5.md5(Common.CreateNo(), 16).ToLower();
                userLogOnEntity.UserPassword = Md5.md5(DESEncrypt.Encrypt(Md5.md5(userLogOnEntity.UserPassword, 32).ToLower(), userLogOnEntity.UserSecretkey).ToLower(), 32).ToLower();
                _ISys_UserRepository.Insert(userEntity);
                _userLogDal.Insert(userLogOnEntity);
            }
           
        }

        public void UpdateForm(Sys_User userEntity)
        {
            _ISys_UserRepository.Update(userEntity);
        }
    }
}